var SWFUInstance = Class.create();

Object.extend(SWFUInstance, {
  instance: false,
  
  set_status: function(file, message) {
    if (file)
      status_container = $('photo_upload_' + file.index).down('div.photo_upload_status');
    else
      status_container = $('upload_status');
   
    if (status_container) {
      status_container.update(message);
    }
  },
  
  file_queued_function: function(file) {
    el = new Element('div', { 'id': 'photo_upload_' + file.index, 'class': 'photo_upload' });
      placeholder_img_container = new Element('div', { 'class': 'photo_upload_placeholder' }).update('&nbsp;');
      upload_container = new Element('div', { 'class': 'photo_upload_info' });
        upload_name = new Element('div', { 'class': 'photo_upload_name' }).update(file.name);
        upload_status = new Element('div', { 'class': 'photo_upload_status' }).update('Waiting...');
      upload_container.appendChild(upload_name);
      upload_container.appendChild(upload_status);
    el.appendChild(placeholder_img_container);
    el.appendChild(upload_container);
    el.appendChild(new Element('br', {'class': 'clear'}));
    
    $('photos-list').insert({bottom: el});
  },
  
  file_dialog_complete_function: function(numFilesSelected, numFilesQueued) {
    photo_set_name = $('photo_set_name').hasClassName('dormant') ? '' : $('photo_set_name').value;
    $('photo_set_name').disable();
    this.addPostParam('photo_set_name', photo_set_name);
    this.addPostParam('photo_set_id', $('photo_set_id').value);
    this.startUpload();
  },
  
  file_queue_error_function: function(file, errorCode, message) {
    switch (errorCode) {
      case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
        SWFUInstance.set_status(file, "Too many files.");
        break;
      case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
        SWFUInstance.set_status(file, "File is too big.");
        break;
      case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
        SWFUInstance.set_status(file, "Cannot upload Zero Byte files.");
        break;
      case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
        SWFUInstance.set_status(file, "Invalid File Type.");
        break;
      default:
        SWFUInstance.set_status(file, "Unhandled Error: " + errorCode + ", message: " + message);
        break;
    }
  },

  upload_start_function: function(file) {
    SWFUInstance.set_status(file, "Uploading...");
    this.addPostParam('photo_set_id', $('photo_set_id').value);
    return true;
  },

  upload_progress_function: function (file, bytesLoaded, bytesTotal) {
    var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
    SWFUInstance.set_status(file, "Uploading... " + percent + "%");
    return true;
  },

  upload_success_function: function (file, serverData) {
    data = eval('(' + serverData + ')');
    
    $('photo_set_id').value = data['photo_set_id'];
    
    img = new Element('img', { 'src': data['thumbnail_path'], 'class': 'thumbnail' });
    
    thumbnail_container = $('photo_upload_' + file.index).down('div.photo_upload_placeholder');
    thumbnail_container.appendChild(img);
    
    name_container = $('photo_upload_' + file.index).down('div.photo_upload_name');
    name_container.update(data['path']);
    
    input = new Element('input', { 'name': 'caption_for_' + data.id, 'class' : 'default' });
    status_container = $('photo_upload_' + file.index).down('div.photo_upload_status');
    status_container.update('');
    status_container.appendChild(input);
    return true;
  },

  upload_error_function: function(file, errorCode, message) {
    switch (errorCode) {
      case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
        SWFUInstance.set_status(file, "Upload Error: " + message);
        break;
      case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
        SWFUInstance.set_status(file, "Upload Failed.");
        break;
      case SWFUpload.UPLOAD_ERROR.IO_ERROR:
        SWFUInstance.set_status(file, "Server (IO) Error");
        break;
      case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
        SWFUInstance.set_status(file, "Security Error");
        break;
      case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
        SWFUInstance.set_status(file, "Upload limit exceeded.");
        break;
      case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
        SWFUInstance.set_status(file, "Failed Validation.  Upload skipped.");
        break;
      case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
        SWFUInstance.set_status(file, "Cancelled");
        break;
      case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
        SWFUInstance.set_status(file, "Stopped");
        break;
      default:
        SWFUInstance.set_status(file, "Unhandled Error: " + errorCode);
        break;
    }
    return true;
  },

  upload_complete_function: function(file) {
    if (this.getStats().files_queued > 0) {
      this.startUpload();
    }
    
    return true;
  },

  // This event comes from the Queue Plugin
  queue_complete_function: function(numFilesUploaded) {
    return true;
  }
  
});

var SWFUSETTINGS = { 
  flash_url: "/swfupload.swf", 
  upload_url: "/admin/photos/upload", 
  debug: false,
  
  // Upload settings
  file_size_limit : "10 MB",
  file_types : "*.jpg;*.gif;*.png",
  file_types_description : "Images",
  file_upload_limit : 100,
  file_queue_limit : 100,
  
  // Button settings
  button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT, 
  button_placeholder_id : "swfu-holder",
  button_width: "52",
  button_height: "16",
  button_text: '<span class="swfu-font">Upload</span>',
  button_text_style: ".swfu-font { font-size: 10px; font-family: Verdana;}",
  button_text_left_padding: 4,
  button_text_right_padding: 4,
  button_text_bottom_padding: 0,
  button_text_top_padding: 0,
  button_disabled : false,
  button_cursor : SWFUpload.CURSOR.HAND,
  
  file_queued_handler : SWFUInstance.file_queued_function,
  file_queue_error_handler : SWFUInstance.file_queue_error_function,
  file_dialog_complete_handler : SWFUInstance.file_dialog_complete_function,
  upload_start_handler : SWFUInstance.upload_start_function,
  upload_progress_handler : SWFUInstance.upload_progress_function,
  upload_error_handler : SWFUInstance.upload_error_function,
  upload_success_handler : SWFUInstance.upload_success_function,
  upload_complete_handler : SWFUInstance.upload_complete_function,
  queue_complete_handler : SWFUInstance.queue_complete_function	// Queue plugin event
};


Event.observe(window, 'load', function() {
  if ($(SWFUSETTINGS.button_placeholder_id))
    SWFUInstance.instance = 
      new SWFUpload(SWFUSETTINGS);
});

